package controllers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import admin.models.CategoryModel;
import admin.models.MetaSeoModel;

import entities.Author;
import entities.Category;
import entities.MetaSeo;
import entities.Question;
import entities.SiteConfig;

import models.AuthorModel;
import models.QuestionModel;

import systems.ConfigSystem;
import systems.RecentlyQuestion;

public class RecentlyServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		SiteConfig config = ConfigSystem.getConfigs();
		
		List<Question> questions = RecentlyQuestion.getQuestions();
		
		List<String> aIds = new ArrayList<String>();
		List<String> cIds = new ArrayList<String>();
		
		for (int i = 0; i < questions.size(); i++) {
			aIds.add(questions.get(i).getAuthor());
			cIds.add(questions.get(i).getCategory());
		}
		
		//Get authors
		AuthorModel authorModel = AuthorModel.getInstance();
		Map<String, Author> authors = authorModel.getByListIds(aIds);
		
		//Get categories
		CategoryModel categoryModel = CategoryModel.getInstance();
		Map<String, Category> categories = categoryModel.getByListIds(cIds);
		
		//Set seo content
		MetaSeoModel metaseoModel = MetaSeoModel.getInstance();
		MetaSeo metaseo = metaseoModel.get("recently");
		
		request.setAttribute("questions", questions);
		request.setAttribute("authors", authors);
		request.setAttribute("categories", categories);
		request.setAttribute("metaseo", metaseo);
		try {
			request.getRequestDispatcher("/templates/" + config.getTemplate() + "/pages/recently.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
		
	}
}
